Repository navigation
Faster real-subspace GPU path: final complex signal from the kernel, one simulation per Jacobian - #58
Merged
Merged
Conversation
…and sample sign folded in The real kernels store each sample as an interleaved complex64 multiplied by a per-sample complex factor, so the forward no longer makes a torch.complex pass and a full-signal multiply after the kernel. A simulator's constant receiver phase (1j for MRF, MPRAGE, MPnRAGE) and the sign the packing moves off the pulses reach the kernel as that factor. The turned flip and phase buffers are remembered per stream, so the pulse-axis check is read once rather than on every call. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
…e on the device The Jacobian batches its directions under vmap, so the signal is simulated once and only the directional derivatives are taken per property. Tissue properties left at a plain number are filled on the device rather than copied from the host one by one. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
… factor Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
With the real forward kernel capped, the 8.0 code is the faster one on an 8.6 card, so a separate 8.6 or 8.9 build buys nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
…y the flip Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QP4KCaPBFdw9c6SoRd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Matteo · project thread
What changed
Simulator.receiver(default1) declares the receiver phase. MRF, MPRAGE and MPnRAGE set it to1jinstead of multiplying the signal inevaluate. Other paths apply the same factor in torch, and the native autograd function carries it through its JVP and VJP.jacobian()batches its directions undervmap, so the signal is simulated once and only the directional derivatives are taken per property.__launch_bounds__(64, 8).No public return type changes. Matteo chose to keep the output complex.
Why
The aim is to match BlochSimulators.jl on the GPU. Profiling MRF-FISP on an A40 showed that a third of the forward was spent outside the kernel: a
torch.complexpass, the1j *multiply inMRFSimulator.evaluate, and host copies and syncs.Validation
A40 (aleatico2), torch 2.13 + CUDA 12.9, MRF-FISP with 500 TRs, 32 states and 1e5 tissues, median of 30 calls:
The signal checksum is identical in every build. A sweep over problems per thread (2, 4, 8), warps per block (2, 4, 8) and resident warps per SM found nothing faster than Y=8, 2 warps, 8 programs per SM, and every variant matched to 0 max error. With the cap, the 8.0 code runs faster on the A40 than an 8.6 build, so the build targets are unchanged.
After this PR, the forward is 99% kernel time. The Jacobian is still behind BlochSimulators.jl: its two dual-number passes take 18.9 ms each. Closing that gap needs a kernel that carries several directions in one sweep, which is left for a follow-up.
CPU, this container:
pytest tests/ -n 4gives 1661 passed, 325 skipped. The other failures are environment-only: a torchvision/torch mismatch (test_deepinv), an old mri-nufft (test_subspace_route), andtest_an_image_quality_design_fits_in_its_budget, which failed under-n 4but passes on its own. The host build of the GPU kernels passestest_host_kernels.pyandtest_real_subspace.py. New test:test_a_fingerprinting_sample_after_an_inversion_is_real_and_turned_by_the_flip(CPU and CUDA).Documentation impact
None for callers: signals and their types are unchanged.
Simulator.receiveris a new class attribute that a subclass can set.Checklist
pytest tests/passes locally, and a new test covers what changed.ruff formatandruff checkare clean.bash scripts/build_docs.shwas not run.Generated by Claude Code